package com.itheima.controller;

import com.itheima.common.AliOssUtils;
import com.itheima.common.R;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * 文件上传控制器
 */
@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliOssUtils aliOssUtils;

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @SneakyThrows //抑制异常
    public R upload(MultipartFile file) {
        log.info("文件上传:{}", file);
        //将file对象转移到指定位置（保存起来）
        //file.transferTo(new File("d:/1.jpg"));
        String url = aliOssUtils.upload(file);
        return R.ok(url);
    }

}
